<?php

namespace app\admin\common\controller;

use app\admin\framework\api\Permission;
use app\common\model\OrderTrip;
use app\common\service\OrderService;
use app\common\service\UserService;
use app\common\utils\Excel;
use think\facade\Db;

#[Permission('始发订单', 'Order', 1)]
class OriginalController
{
    
    private $deptId = 0;
    
    public function __construct()
    {
        $this->deptId = UserService::getValue('dept_id');
    }
    
    
    #[Permission('参数')]
    public function option()
    {
        return apiSuccess(data: [
            'status' => OrderTrip::START_STATUS
        ]);
    }
    
    #[Permission('查询')]
    public function select()
    {
        $keyword = request()->get('keyword', '');
        $status = request()->get('status');
        $list = OrderService::getLists('dept.list', [
            'status' => $status,
            'keyword' => $keyword,
            'identity' => 'start'
        ]);
        return apiSuccess(data: ['list' => $list->items(), 'total' => $list->total()]);
    }
    
    #[Permission('物流追踪')]
    public function logistics()
    {
        $id = request()->get('id', 0);
        return apiSuccess(data: OrderService::getLogistics($id));
    }
    
    #[Permission('验车报告')]
    public function takephone()
    {
        $id = request()->get('id', 0);
        return apiSuccess(data: OrderService::getTrip($id)->photos);
    }
    
    #[Permission('查看合同')]
    public function contract()
    {
    
    }
    
    #[Permission('编辑')]
    public function update()
    {
        $id = request()->post('id', 0);
        $status = request()->post('status', 0);
        $data = request()->post('data');
        OrderService::update($id, $status, $data);
        return apiSuccess('编辑成功');
    }
    
    
    #[Permission('取消合同')]
    public function cancel()
    {
        $id = request()->get('id',0);
        if(OrderService::cancelOrder($id)){
            return apiSuccess('删除成功');
        }else{
            return apiError('删除失败');
        }
    }
    
    
    #[Permission('excel')]
    public function excel()
    { 
        $keyword = request()->get('keyword', ''); 
        $status = request()->get('status');
        $list = OrderService::getLists('dept.list', [ 
            'keyword' => $keyword,
            'status' => $status,
            'identity' => 'start'
        ],true);

        $arr = [];
        foreach ($list as $key => $value){ 
            $boardItem = Db::name('order_board_item')->where('order_id',$value->id)->find(); 
            if(!empty($boardItem)){
                $allcost = Db::name('order_board_item')->where('order_id', $value->id)->sum('total_price');
                $arr[] = [
                    $value->create_time,
                    $value->address,
                    $value->end_address,
                    $value->car_brand,1,
                    $value->pay_type_text,
                    $value->shipper_name,
                    $value->shipper_mobile,
                    $boardItem['landing_price'],
                    $boardItem['transfer_price'],
                    $boardItem['freight_price'],
                    $value->car_shelf_num,
                    bcsub(floatval($value->total_price),$allcost,2)
                ];
            } 
        } 
        
        $filePath = Excel::import('', [
            '日期', '起运地', '到达地', '车辆名称', '台数', '到付', '收车人', '电话', '接车费', '中转费', '付司机运费', '车架号', '单台毛利润'
        ], $arr); 
        return apiSuccess(data: ['url'=>getImageUrl($filePath)]);
    }
    
    
}